//
//  BRRentPreView.m
//  ZEUSPower
//
//  Created by 甘卫国 on 2024/10/16.
//

#import "BRRentPreView.h"

@implementation BRRentPreView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)toPrePay:(QMUIButton *)btn {
    
    [self hiddenSelf];
    
    NSInteger index = btn == self.appleBtn ? 0 : 1;
    
    if (self.prePayClickBlock) self.prePayClickBlock(index);
}


- (void)toPreVsCardPay:(QMUIButton *)btn {
    
    [self hiddenSelf];
        

    
    if (self.cardPayClickBlock) {
        self.cardPayClickBlock();
    }
}



- (void)hiddenSelf {
    [UIView animateWithDuration:0.25 animations:^{
        self.contView.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)showSelf {
    
    self.hidden = NO;
    
    CGFloat h = self.contView.mj_h - 33;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.contView.transform = CGAffineTransformMakeTranslation(0, -h);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)toAddDeposit:(QMUIButton *)btn {
    if (self.toDepositBlock) {
        self.toDepositBlock();
    }
}


- (void)setDepositAmount:(NSDecimalNumber *)depositPreAmount {
    _depositPreAmount = depositPreAmount;
    
    NSString *preTitle = [NSString stringWithFormat:Localized(@"rent_continueDeposit"),[BRPayManager br_amount:depositPreAmount]];
    [self.depositBtn setTitle:preTitle forState:UIControlStateNormal];
}

-(void)setPrepaymentAmount:(NSDecimalNumber *)prepaymentAmount{
    
    _prepaymentAmount = prepaymentAmount;
    
    NSString *preTitle = [NSString stringWithFormat:Localized(@"rent_continueDeposit"),[BRPayManager br_amount:prepaymentAmount]];
    [self.depositBtn setTitle:preTitle forState:UIControlStateNormal];
}



#pragma mark - UI --
- (void)setUI {
    self.hidden = YES;
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.shadowView];
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [self addSubview:self.contView];
    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.mas_bottom).offset(0);
    }];
    
    QMUILabel *titleL = [BRToolsView br_label:Localized(@"rent_chooseAuthMethod") textColor:kColorTitle font:[UIFont br_fontM:14] textAlignment:NSTextAlignmentCenter];
    titleL.numberOfLines = 0;
    [self.contView addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(28);
        make.right.offset(-28);
        
    }];
    
    QMUIButton *close = [[QMUIButton alloc] init];
    [close setImage:UIImageMake(@"arrow_close_gray44") forState:UIControlStateNormal];
    [self.contView addSubview:close];
    [close mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.width.height.mas_equalTo(52);
        make.left.offset(6);
    }];
    [close addTarget:self action:@selector(hiddenSelf) forControlEvents:UIControlEventTouchUpInside];
    
    self.stack = [[UIStackView alloc] init];
    self.stack.axis = UILayoutConstraintAxisVertical;
    self.stack.spacing = 12;
    [self.contView addSubview:self.stack];
    [self.stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleL.mas_bottom).offset(34);
        make.left.offset(28);
        make.right.offset(-28);
        make.bottom.offset(-(33 + kSafeAreaBottomHeight + 16));
    }];
    
    self.labelOr = [BRToolsView br_label:Localized(@"rent_prepaymentOr") textColor:UIColorMakeWithHex(@"#050101") font:[UIFont br_fontB:14] textAlignment:NSTextAlignmentCenter];
    
    
    self.labelOrA = [BRToolsView br_label:Localized(@"rent_prepayment") textColor:UIColorMakeWithHex(@"#AAAAAC") font:[UIFont br_fontM:12] textAlignment:NSTextAlignmentCenter];
    _labelOrA.numberOfLines = 0;
    
    
//    NSString *btnTitle = [NSString stringWithFormat:@"%@ Pay",Localized(@"rent_rentWith")];
//    NSAttributedString *att = [BRStringTools br_att:btnTitle rang:[btnTitle rangeOfString:@"Pay"] font:[UIFont br_fontS:23]];
//     
    self.appleBtn = [[QMUIButton alloc] init];
    [self.appleBtn setTitle:@"Pay" forState:UIControlStateNormal];
//    [self.appleBtn setAttributedTitle:att forState:UIControlStateNormal];
    [self.appleBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.appleBtn.titleLabel.font = [UIFont br_fontS:23];
    [self.appleBtn setBackgroundColor:UIColor.blackColor];
    self.appleBtn.layer.cornerRadius = 26;
    self.appleBtn.layer.masksToBounds = YES;
    
    
    self.vscardBtn = [[QMUIButton alloc] init];
    [self.vscardBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.vscardBtn.titleLabel.font = [UIFont br_fontB:17];
    [self.vscardBtn setBackgroundColor:UIColorMakeWithHex(@"#F5F5F7")];
    self.vscardBtn.layer.cornerRadius = 26;
    self.vscardBtn.layer.masksToBounds = YES;
    
    
    
    UIImageView* carImg = [[UIImageView alloc] init];
    carImg.image = [UIImage imageNamed:@"ic_bankcard"];
    [self.vscardBtn addSubview:carImg];
    
    [carImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(44);
        make.size.mas_equalTo( CGSizeMake(44,44));
        make.centerY.equalTo(self.vscardBtn);
    }];
    
    self.cardLab = [[UILabel alloc] init];
//    self.cardLab.text =@"**** **** **** 0023";
    self.cardLab.font = [UIFont fontS:14];
    [self.vscardBtn addSubview:self.cardLab];
    
    [self.cardLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(carImg.mas_right).offset(14);

        make.centerY.equalTo(self.vscardBtn);
    }];
    
    
    
    self.cardBtn = [[QMUIButton alloc] init];
    [self.cardBtn setTitle:Localized(@"rent_insertCard") forState:UIControlStateNormal];
    [self.cardBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.cardBtn.titleLabel.font = [UIFont br_fontB:17];
    [self.cardBtn setBackgroundColor:kColorMain];
    self.cardBtn.layer.cornerRadius = 26;
    self.cardBtn.layer.masksToBounds = YES;
    
    
    
    
 
   
    
    
    NSString *preTitle = [NSString stringWithFormat:Localized(@"rent_continueDeposit"),@""];
    self.depositBtn = [[QMUIButton alloc] init];
    [self.depositBtn setTitle:preTitle forState:UIControlStateNormal];
    [self.depositBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.depositBtn.titleLabel.font = [UIFont br_fontB:17];
    self.depositBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.depositBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 4, 0, 4);
    [self.depositBtn setBackgroundColor:kColorMain];
    self.depositBtn.layer.cornerRadius = 26;
    self.depositBtn.layer.masksToBounds = YES;
    
    
    [self.stack addArrangedSubview:self.appleBtn];
    
    [self.stack addArrangedSubview:self.vscardBtn];
    [self.stack addArrangedSubview:self.cardBtn];

//    [self.stack addArrangedSubview:_labelOr];
//    [self.stack addArrangedSubview:_labelOrA];
//    [self.stack addArrangedSubview:self.depositBtn];
    
    
    [self.stack addSubview:self.recordView];
    [self bringSubviewToFront:self.recordView];
    
    self.recordView.hidden = YES;
    [self.recordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.vscardBtn.mas_right).offset(-24);
//        make.top.offset(-10);
        make.top.equalTo(self.vscardBtn).offset(-10);
    }];
    
    
    
    
    
    [self.appleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
    }];
    
    
    [self.stack addSubview:self.recordPayView];
    [self bringSubviewToFront:self.recordPayView];
    [self.recordPayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.appleBtn.mas_right).offset(-24);
//        make.top.offset(-10);
        make.top.equalTo(self.appleBtn).offset(-10);
    }];
    
    self.recordPayView.hidden = YES;
    
    [self.cardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
    }];
    
    
    
    [self.stack addSubview:self.recordAddCardView];
    [self bringSubviewToFront:self.recordAddCardView];
    [self.recordAddCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.cardBtn.mas_right).offset(-24);
//        make.top.offset(-10);
        make.top.equalTo(self.cardBtn).offset(-10);
    }];
    self.recordAddCardView.hidden = YES;
    
    [self.vscardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
    }];
    
    [self.depositBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
    }];
   
    [self.labelOr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_lessThanOrEqualTo(16);
        
    }];
    
    [self.labelOrA mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_lessThanOrEqualTo(60);
        
    }];
    
    
    [self.vscardBtn addTarget:self action:@selector(toPreVsCardPay:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.appleBtn addTarget:self action:@selector(toPrePay:) forControlEvents:UIControlEventTouchUpInside];
    [self.cardBtn addTarget:self action:@selector(toPrePay:) forControlEvents:UIControlEventTouchUpInside];
    [self.depositBtn addTarget:self action:@selector(toAddDeposit:) forControlEvents:UIControlEventTouchUpInside];
    
    WS(weakSelf);
    [self.shadowView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [weakSelf hiddenSelf];
    }];


    [self.contView layoutIfNeeded];
    
    
}


-(void)ShowRecently:(BOOL)hasCardModel {
    NSString *defaultKey = [[NSUserDefaults standardUserDefaults] objectForKey:RecentlyusedcardKey];
    
    NSLog(@"RecentlyusedcardKey =====%@",defaultKey);
    
    BOOL hasCard = [defaultKey isEqualToString:@"RecentlyusedcardKey"];//使用卡的情况
    BOOL hasApple = [defaultKey isEqualToString:@"RecentlyusedcardKeyapple"];//使用苹果支付的情况
    BOOL hasCardAdd = [defaultKey isEqualToString:@"RecentlyusedaddcardKey"];//使用添加卡支付的情况
    
    if (!hasCardModel && hasCard) {
        hasCardAdd = YES;
        hasCard = NO;
    }
    
    self.recordView.hidden = !hasCard;
    self.recordPayView.hidden = !hasApple;
    self.recordAddCardView.hidden = !hasCardAdd;
   
}


-(void)changeVSCardHeight{
    
    [self.vscardBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
    }];
    
    
    
    NSString *defaultKey = [[NSUserDefaults standardUserDefaults] objectForKey:RecentlyusedcardKey];
    
    NSLog(@"RecentlyusedcardKey =====%@",defaultKey);
    
    if ([defaultKey isEqualToString:@"RecentlyusedcardKey"]) {
        self.recordView.hidden = NO;
    }else if([defaultKey isEqualToString:@"RecentlyusedcardKeyapple"]){
  
//        [self.recordView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(self.appleBtn.mas_right).offset(-24);
//    //        make.top.offset(-10);
//            make.top.equalTo(self.appleBtn).offset(-10);
//        }];
//        
//        self.recordView.hidden = NO;
        
    }else{

    }
    
    
    

    [self.stack layoutIfNeeded];

    [self.contView layoutIfNeeded];
}


-(void)setCardModel:(BRCardModel *)cardModel{
    
    if (cardModel) {
        self.cardLab.text =[NSString stringWithFormat:@"**** **** **** %@",cardModel.bankNo];
    }
}

- (UIView *)shadowView {
    
    if (_shadowView == nil) {
        _shadowView = [[UIView alloc] init];
        _shadowView.backgroundColor = kColorShadow(0.75);
    }
    return _shadowView;
}

- (UIView *)contView {
    
    if (_contView == nil) {
        _contView = [[UIView alloc] init];
        _contView.backgroundColor = UIColor.whiteColor;
        _contView.layer.cornerRadius = 33;
        _contView.layer.masksToBounds = YES;
    }
    return _contView;
}

- (BRLoginRecordView *)recordView {
    
    if (_recordView == nil) {
        _recordView = [[BRLoginRecordView alloc] init];
        _recordView.contV.backgroundColor = kUIColorMainTint;
        _recordView.titleL.textColor = kColorMain;
        _recordView.bImage.hidden = YES;

    }
    return _recordView;
}


- (BRLoginRecordView *)recordPayView {
    
    if (_recordPayView == nil) {
        _recordPayView = [[BRLoginRecordView alloc] init];
        _recordPayView.contV.backgroundColor = kUIColorMainTint;
        _recordPayView.titleL.textColor = kColorMain;
        _recordPayView.bImage.hidden = YES;
    }
    return _recordPayView;
}


- (BRLoginRecordView *)recordAddCardView {
    
    if (_recordAddCardView == nil) {
        _recordAddCardView = [[BRLoginRecordView alloc] init];
        _recordAddCardView.contV.backgroundColor = kUIColorMainTint;
        _recordAddCardView.titleL.textColor = kColorMain;
        _recordAddCardView.bImage.hidden = YES;

    }
    return _recordAddCardView;
}

@end
